package bb.lanxing.lib.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Build;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import androidx.appcompat.widget.AppCompatTextView;

import bb.lanxing.R;

public class AutoResizeTextView extends AppCompatTextView {
    private static final int NO_LINE_LIMIT = -1;
    private boolean autoResize;
    private final RectF availableSpaceRect;
    private boolean fitFontHeight;
    private boolean initialized;
    private final TextPaint mTempPaint;
    private int maxLines;
    private float maxTextSize;
    private float minTextSize;
    private final SizeTester sizeTester;
    private float spacingAdd;
    private float spacingMult;
    private int widthLimit;

    protected boolean isValidWordWrap(char c, char c2) {
        return c == ' ' || c == '-';
    }

    public AutoResizeTextView(Context context) {
        this(context, null, 0);
    }

    public AutoResizeTextView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public AutoResizeTextView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.availableSpaceRect = new RectF();
        this.spacingMult = 1.0f;
        this.spacingAdd = 0.0f;
        this.initialized = false;
        this.autoResize = false;
        this.fitFontHeight = true;
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.AutoResizeTextView, i, 0);
        this.minTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AutoResizeTextView_minFontSize,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12.0f, getResources().getDisplayMetrics()));
        this.autoResize = obtainStyledAttributes.getBoolean(R.styleable.AutoResizeTextView_autoResize, this.autoResize);
        this.fitFontHeight = obtainStyledAttributes.getBoolean(R.styleable.AutoResizeTextView_fitFontHeight, this.fitFontHeight);
        float textSize = getTextSize();
        this.maxTextSize = textSize;
        if (this.minTextSize > textSize) {
            this.minTextSize = textSize / 2.0f;
        }
        obtainStyledAttributes.recycle();
        this.mTempPaint = new TextPaint(getPaint());
        if (this.maxLines == 0) {
            this.maxLines = -1;
        }
        this.sizeTester = new SizeTester() {
            final RectF textRect = new RectF();

            @Override
            public int onTestSize(int i2, RectF rectF) {
                String charSequence;
                AutoResizeTextView.this.mTempPaint.setTextSize(i2);
                TransformationMethod transformationMethod = AutoResizeTextView.this.getTransformationMethod();
                if (transformationMethod != null) {
                    charSequence = transformationMethod.getTransformation(AutoResizeTextView.this.getText(), AutoResizeTextView.this).toString();
                } else {
                    charSequence = AutoResizeTextView.this.getText().toString();
                }
                if (AutoResizeTextView.this.getMaxLines() == 1 || (AutoResizeTextView.this.getMaxLines() == -1 && AutoResizeTextView.this.getLineCount() <= 1)) {
                    this.textRect.bottom = (Build.VERSION.SDK_INT < 16 || AutoResizeTextView.this.getIncludeFontPadding()) ? AutoResizeTextView.this.mTempPaint.getFontSpacing() : AutoResizeTextView.this.mTempPaint.getTextSize();
                    this.textRect.right = AutoResizeTextView.this.mTempPaint.measureText(charSequence);
                } else {
                    StaticLayout staticLayout = new StaticLayout(charSequence, AutoResizeTextView.this.mTempPaint, AutoResizeTextView.this.widthLimit, Layout.Alignment.ALIGN_NORMAL, AutoResizeTextView.this.spacingMult, AutoResizeTextView.this.spacingAdd, true);
                    if (AutoResizeTextView.this.getMaxLines() != -1 && staticLayout.getLineCount() > AutoResizeTextView.this.getMaxLines()) {
                        return 1;
                    }
                    this.textRect.bottom = staticLayout.getHeight();
                    int lineCount = staticLayout.getLineCount();
                    int i3 = -1;
                    for (int i4 = 0; i4 < lineCount; i4++) {
                        int lineEnd = staticLayout.getLineEnd(i4);
                        if (i4 < lineCount - 1 && lineEnd > 0 && !AutoResizeTextView.this.isValidWordWrap(charSequence.charAt(lineEnd - 1), charSequence.charAt(lineEnd))) {
                            return 1;
                        }
                        if (i3 < staticLayout.getLineRight(i4) - staticLayout.getLineLeft(i4)) {
                            i3 = ((int) staticLayout.getLineRight(i4)) - ((int) staticLayout.getLineLeft(i4));
                        }
                    }
                    this.textRect.right = i3;
                }
                this.textRect.offsetTo(0.0f, 0.0f);
                if (rectF.contains(this.textRect)) {
                    return (rectF.height() - this.textRect.height() <= 1.0f || rectF.width() - this.textRect.width() <= 1.0f) ? 0 : -1;
                }
                return 1;
            }
        };
        this.initialized = true;
    }

    @Override
    public void setAllCaps(boolean z) {
        super.setAllCaps(z);
        if (this.autoResize) {
            adjustTextSize();
        }
    }

    @Override
    public void setTypeface(Typeface typeface) {
        super.setTypeface(typeface);
        if (this.autoResize) {
            adjustTextSize();
        }
    }

    @Override
    public void setMaxLines(int i) {
        super.setMaxLines(i);
        if (this.autoResize) {
            this.maxLines = i;
            adjustTextSize();
        }
    }

    @Override
    public int getMaxLines() {
        return this.maxLines;
    }

    @Override
    public void setSingleLine() {
        super.setSingleLine();
        if (this.autoResize) {
            this.maxLines = 1;
            adjustTextSize();
        }
    }

    @Override
    public void setSingleLine(boolean z) {
        super.setSingleLine(z);
        if (this.autoResize) {
            if (z) {
                this.maxLines = 1;
            } else {
                this.maxLines = -1;
            }
            adjustTextSize();
        }
    }

    @Override
    public void setLines(int i) {
        super.setLines(i);
        if (this.autoResize) {
            this.maxLines = i;
            adjustTextSize();
        }
    }

    @Override
    public void setTextSize(float f) {
        if (this.autoResize) {
            if (this.maxTextSize == f) {
                return;
            }
            this.maxTextSize = f;
            adjustTextSize();
            fitWithFontHeight(f);
            return;
        }
        super.setTextSize(f);
    }

    @Override
    public void setTextSize(int i, float f) {
        Resources resources;
        if (this.autoResize) {
            Context context = getContext();
            if (context == null) {
                resources = Resources.getSystem();
            } else {
                resources = context.getResources();
            }
            float applyDimension = TypedValue.applyDimension(i, f, resources.getDisplayMetrics());
            if (applyDimension == this.maxTextSize) {
                return;
            }
            this.maxTextSize = applyDimension;
            adjustTextSize();
            fitWithFontHeight(applyDimension);
            return;
        }
        super.setTextSize(i, f);
    }

    private void fitWithFontHeight(float f) {
        if (this.fitFontHeight) {
            ViewGroup.LayoutParams layoutParams = getLayoutParams();
            this.mTempPaint.setTextSize(f);
            int round = Math.round(this.mTempPaint.getFontSpacing());
            if (layoutParams.height == round) {
                return;
            }
            layoutParams.height = round;
        }
    }

    @Override
    public void setLineSpacing(float f, float f2) {
        super.setLineSpacing(f, f2);
        this.spacingMult = f2;
        this.spacingAdd = f;
    }

    private void adjustTextSize() {
        int measuredHeight;
        if (!this.initialized) {
            return;
        }
        int i = (int) this.maxTextSize;
        int measuredWidth = (getMeasuredWidth() - getCompoundPaddingLeft()) - getCompoundPaddingRight();
        this.widthLimit = measuredWidth;
        if (measuredWidth <= 0) {
            return;
        }
        this.mTempPaint.set(getPaint());
        if (getLayoutParams().height == -2) {
            this.mTempPaint.setTextSize(this.maxTextSize);
            measuredHeight = Math.round(this.mTempPaint.getFontSpacing());
        } else {
            measuredHeight = (getMeasuredHeight() - getCompoundPaddingBottom()) - getCompoundPaddingTop();
        }
        this.availableSpaceRect.right = this.widthLimit;
        this.availableSpaceRect.bottom = measuredHeight;
        superSetTextSize(i);
    }

    private void superSetTextSize(int i) {
        super.setTextSize(0, binarySearch((int) this.minTextSize, i, this.sizeTester, this.availableSpaceRect));
    }

    private int binarySearch(int i, int i2, SizeTester sizeTester, RectF rectF) {
        int i3 = i2;
        int i4 = i;
        while (i <= i3) {
            i4 = (i + i3) >>> 1;
            int onTestSize = sizeTester.onTestSize(i4, rectF);
            if (onTestSize >= 0) {
                if (onTestSize <= 0) {
                    break;
                }
                i4--;
                i3 = i4;
            } else {
                int i5 = i4 + 1;
                i4 = i;
                i = i5;
            }
        }
        return i4;
    }

    @Override
    protected void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        super.onTextChanged(charSequence, i, i2, i3);
        if (this.autoResize) {
            adjustTextSize();
        }
    }

    @Override
    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        if (this.autoResize) {
            if (i == i3 && i2 == i4) {
                return;
            }
            adjustTextSize();
        }
    }
}
